/**
 * 合同列表
 * @author linfeng
 * @since 2020/05/09
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    let layDate = layui.laydate;
    var layedit = layui.layedit;

    layDate.render({
        elem: '#signDay',
        range: true
    });

    form.render();

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        defaultToolbar: json.showExportExcel ? ['exports'] : [''],
        limit: json.pageSize,
        height: 'full-240',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
    });

    //查询条件重置
    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-contract-filter", {
            "nameOrId": "" // "name": "value"
            ,"contractType": ""
            ,"signBeginAndEndDay": ''
            ,"secret": ""
            ,"agentCompanyId": ""
            ,"agentDeptOneId": ""
            ,"agentDeptTwoId": ""
            ,"performance": ""
            ,"paymentStatus": ""
            ,"status": ""
            ,"advent": ""
            ,"subject": ""
        });
        return false;
    });

    // 监听导出事件
    form.on('submit(query-export)', function (data) {
        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });
        zmnUtil.downloadLimitByConfig("/contract/record/exportExcel.action?" + $("#queryForm").serialize(), "合同管理列表.xlsx", function () {
            layer.close(index);
        });
        return false;
    });

    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    // 2级部门联动
    form.on('select(deptOneId)', function(obj){
        triggerTwoDeptOptions(obj.value);
    });

    function triggerTwoDeptOptions(value){
        let twoDeptOptions = json.twoDepts.filter(p => p.parentId == value).map(p => {
            return p.deptId == json.vo.deptTwoId ? '<option value="'+p.deptId+'" selected="selected">'+p.deptName+'</option>' : '<option value="'+p.deptId+'">'+p.deptName+'</option>';
        });
        twoDeptOptions.unshift('<option value=""></option>');
        $('#deptTwoId').html(twoDeptOptions);
        form.render("select");
    }

    // 修改排序和首字母
    table.on("edit(zmn-table)", function (obj) {
        if(obj.field=='sort'){
            zmnPage.laySortPositiveInteger(obj.value,"/contract/record/updateSort.action",obj.data.contractId,100000,1000);
        }
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        // 表格导出
        if (obj.event == 'CUSTOM_EXPORT') {
            $("#query-export").click();
        }
    });

    //对外暴露的接口
    exports('contract_record_list_js', {});
});

/**
 * 描述: 打开新增合同借阅记录页面
 **/
function addContractBorrowRecord(contractId, file, fileSrc){
    zmnLayUtil.openIframeCustom(layer, "/contract/borrow/record/detail.action?contractId="+contractId+"&file="+file+"&fileSrc="+fileSrc, {title: "新增借阅记录", shade: 0.8, area:["550px", "490px"]});
}

